package mi.payment.utils.transaction;

import android.content.Context;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import mi.payment.bean.db.TradeData;
import mi.payment.constants.ParseUtils;
import mi.payment.constants.TransactionBitmap;
import mi.payment.utils.iso8583.EncodePackage;
import mi.payment.utils.iso8583.FieldUtils;
import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.secure.TerminalSecure;
import sunmi.sunmiui.utils.LogUtil;

/**
 * 二维码轮询
 *
 * @author tomcat on 2017/2/17.
 */
public class QRCodeLoopingTask extends TransactionTask<TradeData, Void, TradeData> {

    protected QRCodeLoopingTask(Context context) {
        super(context, true);
    }

    @Override
    protected void onBegin(TradeData[] params) {
        tradeData = params[0];
        dbBean = tradeData.getPayDetail();
    }

    @Override
    protected TradeData appointResult() {
        return tradeData;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        return EncodePackage.preAssemblyBase(TransactionBitmap.BITMAP_QR_CODE_PASSIVE);
    }

    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {
        dbBean.setProcessCode("670000");
        dbBean.setTransType("01");
        dbBean.setNetManageCode("000");
        dbBean.setTerminalDate(System.currentTimeMillis() / 1000);

        Map<String, MsgField> map = FieldUtils.fillEachFieldValue(fieldMap, tradeData);

        // 填写22域
        String val022;
        // 用法描述 031:反扫 032:正扫
        if (dbBean.qrCodeScanModel == 1) {
            val022 = "032";
        } else {
            val022 = "031";
        }
        MsgField fieldF022 = map.get("F022");
        fieldF022.setDataStr(val022);
        fieldF022.setDataLength(val022.length());

        // 填写47域
        // 通道类型 微信-TX01 盛付通钱包-SFT01 支付宝-ZFB01
        String channelType = "TX01";
        if (dbBean.transPlatform == 1) {
            channelType = "ZFB01";
        } else if (dbBean.transPlatform == 2) {
            channelType = "TX01";
        } else if (dbBean.transPlatform == 4) {
            channelType = "CUP01";
        }
        MsgField fieldF047 = map.get("F047");
        StringBuilder value47 = new StringBuilder();
        value47.append("TXNWAY=");
        value47.append(channelType);
        fieldF047.setDataStr(value47.toString());
        fieldF047.setDataLength(value47.length());

        // 填写60域
        MsgField fieldF060 = map.get("F060");
        String value60 = dbBean.getTransType() + dbBean.getBathNo() + dbBean.getNetManageCode();
        fieldF060.setDataStr(value60);
        fieldF060.setDataLength(value60.length());

        // 填写61域
        MsgField fieldF061 = map.get("F061");
        StringBuilder f61SB = new StringBuilder();
        f61SB.append(dbBean.getOriginalBathNo());
        f61SB.append(dbBean.getOriginalPOSNum());
        fieldF061.setDataStr(f61SB.toString());
        fieldF061.setDataLength(f61SB.length());

        // 添加自定义的商户号
        map = ParseUtils.addCustomTransID(map);

        return EncodePackage.assembly(map, "0200", TerminalSecure.PROCESSREQUEST_NONE);
    }

    @Override
    protected void onUnpacked(HashMap<String, MsgField> resp) {
        // 将二维码查询时的流水赋值给成功的查询
        LogUtil.e("nsz", "QRCodeQueryRetTask原流水号:" + dbBean.originalPOSNum);
        dbBean.voucherNo = dbBean.originalPOSNum;
        if ("00".equals(resultStr)) {
            dbBean.QRCodeTransState = 1;
            MsgField f037 = resp.get("F037");
            if (f037 != null) {
                dbBean.thirdTransNum = f037.getDataStr();
            }
        } else if ("A7".equals(resultStr) || "AA".equals(resultStr) || "AW".equals(resultStr)) {
            dbBean.QRCodeTransState = 2;
        } else {
            dbBean.QRCodeTransState = -1;
        }
        MsgField f044 = resp.get("F044");
        if (f044 != null) {
            byte[] bytes = f044.getDataBytes();
            try {
                String res = new String(bytes, "gbk").trim();
                LogUtil.e(TAG, "F044 = " + res);
                dbBean.setTradeResultDes(res);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

}
